let testId = '',
  stuId = '';

function loadPage() {
  testId = localStorage.getItem('testId');
  stuId = localStorage.getItem('stuId');
  $.get(`${app.urlLocation.queryApByTestId}`, {testId: testId, stuId: stuId}, res => {
    console.info(res);
    let subject = res[0];
    $('.paper-title').text(subject.testTitle);
    $('.paper-class-type').text(subject.classType);
    $('.scale span').text(subject.sumScale);
    writeSubjectCard(res);
  })
}

function writeSubjectCard(subjects) {
  let subjectHtml = '';
  $.each(subjects, (i, v) => {
    subjectHtml += writeSubjectHeader(v);
    let subjectContent = v.subjectContent;
    let subjectType = v.subjectType;
    let subjectContentArr;
    switch (subjectType) {
      case 1:
        subjectContentArr = subjectContent.split('|');
        $.each(subjectContentArr, (j, e) => {
          let pre = String.fromCharCode(65 + j);
          subjectHtml += returnOptionHtml(pre, e, j + 1);
        });
        break;
      case 2:
        subjectContentArr = subjectContent.split('|');
        $.each(subjectContentArr, (j, e) => {
          let pre = String.fromCharCode(65 + j);
          subjectHtml += returnOptionHtml(pre, e, j + 1);
        });
        break;
    }
    subjectHtml += `</div>`;
    subjectHtml += returnSubjectDetail(v.subjectAnswer, v.refAnswer);
    subjectHtml += `</div>`;
  });
  $('.test-content').append(subjectHtml);
}

function writeSubjectHeader(subject) {
  return `<div class="layui-card">` +
    `<div class="layui-card-header">` +
    `<span class="layui-form-label">第` +
    `<span class="question-no">${subject.subjectNo}</span>题</span>` +
    `<span class="question-content">${subject.subjectTitle}</span>` +
    `<span class="question-type layui-hide">${subject.subjectType}</span></div>` +
    `<div class="layui-card-body">`;
}

function returnOptionHtml(pre, option, index) {
  return `<div class="layui-input-block"><span class="option${index}">${pre}.&nbsp;&nbsp;${option}</span></div>`;
}

function returnSubjectDetail(answer, refAnswer) {
  let answerHtml = '';
  let refAnswerHtml = '';
  if (answer.indexOf('|')) {
    let answerArr = answer.split('|');
    $.each(answerArr, (i, v) => {
      answerHtml += String.fromCharCode(65 + parseInt(v));
    });
    let refAnswerArr = refAnswer.split('|');
    $.each(refAnswerArr, (i, v) => {
      refAnswerHtml += String.fromCharCode(65 + parseInt(v));
    });
  } else {
    answerHtml += String.fromCharCode(65 + parseInt(answer));
    refAnswerHtml += String.fromCharCode(65 + parseInt(refAnswer));
  }
  return `<div class="details">` +
    `<p class="student-answer">答案：<span>${answerHtml}</span></p>` +
    `<p class="answer">参考答案：<span>${refAnswerHtml}</span></p>` +
    `</div>`;
}


function returnPreviousPage() {
  localStorage.removeItem('testId');
  window.location.href = '/student-action/index';
}